package top.codestudy.wanandroid.request;


import android.util.Log;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitUtil {
    private static String TAG = "RetrofitUtil";
    private static String BASE_URL = "https://www.wanandroid.com/";

    private static volatile Retrofit retrofit = null;

    public synchronized static Retrofit getRetrofitInstance() {
        if (retrofit == null) {
            OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
            OkHttpClient client = httpBuilder.readTimeout(300, TimeUnit.MILLISECONDS)
                    .connectTimeout(300, TimeUnit.MILLISECONDS).writeTimeout(300, TimeUnit.MILLISECONDS)
                    .build();

            retrofit = new Retrofit.Builder()
                    .client(client)
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        Log.i(TAG, "getRetrofitInstance: retrofit ==> " + retrofit);
        return retrofit;
    }

    public static WanAndroidApi getWanAndroidApi() {
        Log.i(TAG, "getWanAndroidApi: ");
        return getRetrofitInstance().create(WanAndroidApi.class);
    }
}
